package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class ListAddDelMultiEx extends Activity {
	ArrayList<String> Items;
	ArrayAdapter<String> Adapter;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listadddeltest);

		Items = new ArrayList<String>();
		Items.add("First");
		Items.add("Second");
		Items.add("Third");
		Items.add("Fourth");
		Items.add("Fifth");

		Adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, Items);

		list = (ListView) findViewById(R.id.listadddellist);
		list.setAdapter(Adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}

	public void mOnClick(View v) {
		EditText et = (EditText) findViewById(R.id.listadddelet);
		switch (v.getId()) {
		case R.id.listadddelbtn1:
			String text = et.getText().toString();
			if (text.length() != 0) {
				Items.add(text);
				et.setText("");
				Adapter.notifyDataSetChanged();
			}
			break;

		case R.id.listadddelbtn2:
			SparseBooleanArray sb = list.getCheckedItemPositions();
			if (sb.size() != 0) {
				for (int i = list.getCount() - 1; i >= 0; i--) {
					if(sb.get(i)){
						Items.remove(i);
					}
				}
				list.clearChoices();
				Adapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
